package com.taptech.doufu.presenter.home;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.bean.novel.ChatNovelCategoryBean;
import com.taptech.doufu.bean.novel.ChatNovelCategoryGsonBean;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.util.GsonUtil;

import org.json.JSONObject;

import java.io.File;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Wyatt on 2017/10/13/013.
 */

public class ChatNovelCategoryPresenter {

    public interface OnHomeCategoryView{

        void onCategoryNext(List<ChatNovelCategoryBean.Bean> list, boolean isCache);
    }

    public ChatNovelCategoryPresenter(Context context,OnHomeCategoryView onHomeCategoryView){
        this.onHomeCategoryView = onHomeCategoryView;
        mContext = context;
    }

    private OnHomeCategoryView onHomeCategoryView;
    private Context mContext;

    /**
     * 获取对话小说缓存
     */
    public void loadCNCategoryCache() {
        Observable.create(new Observable.OnSubscribe<ChatNovelCategoryGsonBean>() {
                    @Override
                    public void call(Subscriber<? super ChatNovelCategoryGsonBean> subscriber) {
                        JSONObject json = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_novel_chat_category));
                        ChatNovelCategoryGsonBean node = GsonUtil.parseJsonWithGson(json.toString(), ChatNovelCategoryGsonBean.class);
                        subscriber.onNext(node);
                    }
                })
                .compose(RxJavaHelper.<ChatNovelCategoryGsonBean>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<ChatNovelCategoryGsonBean>(){
                    @Override
                    public void onNext(ChatNovelCategoryGsonBean node) {
                        super.onNext(node);
                        if (onHomeCategoryView != null && node != null&& node.getData()!=null) {
                            onHomeCategoryView.onCategoryNext(node.getData().getList(),false);
                        }
                    }
                });

    }

    /**
     * 获取分类数据
     */
    public void getHomeNovelCategory(boolean isUseCache) {
        ApiClient.getInstance().setUseCache(isUseCache).getService().getChatNovelCategory()
                .map(new Func1<ChatNovelCategoryGsonBean, ChatNovelCategoryGsonBean>() {
                    @Override
                    public ChatNovelCategoryGsonBean call(ChatNovelCategoryGsonBean chatNovelCategoryGsonBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(chatNovelCategoryGsonBean),new File(CommmonImp.Home_novel_chat_category));
                        return chatNovelCategoryGsonBean;
                    }

                })
                .compose(RxJavaHelper.<ChatNovelCategoryGsonBean>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<ChatNovelCategoryGsonBean>() {
                    @Override
                    public void onNext(ChatNovelCategoryGsonBean novelHomeCategoryBean) {
                        super.onNext(novelHomeCategoryBean);
                        if (onHomeCategoryView != null && novelHomeCategoryBean != null) {
                            onHomeCategoryView.onCategoryNext(novelHomeCategoryBean.getData().getList(),false);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }


}
